package com.fgou.springcloud.controller;

import com.fgou.springcloud.pojo.Dept;
import com.fgou.springcloud.service.dept.DeptService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 99
 */
@RestController
@RequestMapping(value = "/dept",produces = "application/json; charset=UTF-8")
public class DeptController {

    @Resource(name = "deptService")
    DeptService deptService;

    @GetMapping("/list")
    public List<Dept> getDeptAll() {
        return deptService.getDeptAll();
    }

    @GetMapping("/get/{id}")
    public Dept getDeptById(@PathVariable(value = "id") Long id) {
        Dept deptById = deptService.getDeptById(id);
//        为空抛出运行时异常
        if (deptById==null){
            throw new RuntimeException();
        }
        return deptById;
    }

    @PostMapping("/add")
    public Boolean addDeptOne(@RequestBody Map<String, String> map) {
        System.out.println(map.get("deptName"));
        Dept dept = new Dept().setDeptName(map.get("deptName"));
        return deptService.addDept(dept);
    }

}
